package com.n.facedetectordemo;

import android.util.Log;

import okhttp3.logging.HttpLoggingInterceptor;

/**
 * 日志拦截器
 */
public class LoggingInterceptor {
    private HttpLoggingInterceptor mLogging;
    private static int showLength = 4000;

    public LoggingInterceptor() {
        // 测试环境默认设置的日志等级为BODY，正式环境禁止打印
        this(HttpLoggingInterceptor.Level.BODY);
    }

    public LoggingInterceptor(HttpLoggingInterceptor.Level level) {
        mLogging = new HttpLoggingInterceptor(
                new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        showLargeLog("Retrofit", message);
                    }
                });

        mLogging.setLevel(level);
    }

    public HttpLoggingInterceptor getLogging() {
        return mLogging;
    }

    public void setLogging(HttpLoggingInterceptor logging) {
        mLogging = logging;
    }

    /**
     * 分段打印出较长log文本
     *
     * @param logContent 打印文本
     * @param tag        打印log的标记
     */

    public static void showLargeLog(String tag, String logContent) {
        for (int i = 0; i < logContent.length(); i = i + showLength) {
            int e = Math.min(i + showLength, logContent.length());
            String show = logContent.substring(i, e);
            Log.d(tag, show);
        }
    }
}
